<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>业务管理 - 编辑业务</title>
    <link rel="stylesheet" href="../css/style.css">
    <script src="../js/jquery-3.7.1.min.js"></script>
    <script src="../js/utils.js"></script>
    <script src="../js/api.js"></script>
    <script type="module" src="../js/components/navigation.js"></script>
    <style>
        .form-container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .form-title {
            text-align: center;
            margin-bottom: 30px;
            color: #2d3436;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #2d3436;
        }
        .form-group input,
        .form-group select,
        .form-group textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        .action-buttons {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-top: 30px;
        }
        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        .btn-primary {
            background-color: #3a56d4;
            color: white;
        }
        .btn-primary:hover {
            background-color: #2a46c4;
        }
        .btn-secondary {
            background-color: #6c757d;
            color: white;
            text-decoration: none;
            display: inline-block;
            text-align: center;
            line-height: 1.5;
        }
        .btn-secondary:hover {
            background-color: #5a6268;
        }
    </style>
</head>
<body>
<div class="app-container">
    <header class="app-header">
        <div class="logo">员工管理系统</div>
        <div class="user-info">
            <span id="username-display">管理员</span>
            <button id="logout-btn" class="btn btn-secondary">退出登录</button>
        </div>
    </header>

    <div class="app-body">
        <aside class="sidebar">
            <nav class="menu">
                <ul>
                    <li><a href="#" class="menu-item active" data-target="welcome">欢迎页</a></li>
                    <li class="menu-group">
                        <a href="#" class="menu-item">员工管理</a>
                        <ul class="submenu">
                            <li><a href="../employee/list.html" class="submenu-item">员工列表</a></li>
                            <li><a href="../employee/add.html" class="submenu-item">添加员工</a></li>
                        </ul>
                    </li>
                    <li class="menu-group">
                        <a href="#" class="menu-item">业务管理</a>
                        <ul class="submenu">
                            <li><a href="list.html" class="submenu-item">业务列表</a></li>
                            <li><a href="create.html" class="submenu-item">创建业务</a></li>
                        </ul>
                    </li>
                    <li class="menu-group">
                        <a href="#" class="menu-item">薪酬管理</a>
                        <ul class="submenu">
                            <li><a href="../salary/overview.html" class="submenu-item">薪酬概览</a></li>
                            <li><a href="../salary/manage.html" class="submenu-item">薪酬管理</a></li>
                        </ul>
                    </li>
                    <li class="menu-group">
                        <a href="#" class="menu-item">系统设置</a>
                        <ul class="submenu">
                            <li><a href="../system/user.html" class="submenu-item">用户管理</a></li>
                            <li><a href="../system/profile.html" class="submenu-item">个人设置</a></li>
                        </ul>
                    </li>
                </ul>
            </nav>
        </aside>

        <main class="content">
            <div class="form-container">
                <h2 class="form-title">编辑业务</h2>
                <form id="update-business-form">
                    <input type="hidden" id="bId" name="bId">

                    <div class="form-group">
                        <label for="bName">业务名称 <span style="color: red;">*</span></label>
                        <input type="text" id="bName" name="bName" placeholder="请输入业务名称">
                    </div>
                    <div class="form-group">
                        <label for="description">业务描述</label>
                        <textarea id="description" name="description" rows="4" placeholder="请输入业务描述"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="startDateStr">开始日期 <span style="color: red;">*</span></label>
                        <input type="date" id="startDateStr" name="startDateStr">
                    </div>
                    <div class="form-group">
                        <label for="endDateStr">结束日期 <span style="color: red;">*</span></label>
                        <input type="date" id="endDateStr" name="endDateStr">
                    </div>
                    <div class="form-group">
                        <label for="status">状态 <span style="color: red;">*</span></label>
                        <select id="status" name="status">
                            <option value="">请选择状态</option>
                            <option value="未开始">未开始</option>
                            <option value="进行中">进行中</option>
                            <option value="已完成">已完成</option>
                            <option value="已暂停">已暂停</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="money">预计收入 <span style="color: red;">*</span></label>
                        <input type="number" id="money" name="money" placeholder="请输入预计收入" step="0.01">
                    </div>
                    <div class="action-buttons">
                        <button type="submit" class="btn btn-primary">保存</button>
                        <a href="list.html" class="btn btn-secondary">取消</a>
                    </div>
                </form>
            </div>
        </main>
    </div>
</div>

<script>
    $(document).ready(function () {
        // 从URL获取业务ID并设置到隐藏字段
        const urlParams = new URLSearchParams(window.location.search);
        const businessId = urlParams.get('bId');

        if (businessId) {
            $('#bId').val(businessId);
            // 加载业务数据
            loadBusinessData(businessId);
        } else {
            alert('未找到业务ID');
            window.location.href = 'list.html';
        }

        // 表单提交事件
        $('#update-business-form').on('submit', function(e) {
            e.preventDefault();

            if (validateForm()) {
                updateBusiness();
            }
        });
    });

    // 加载业务数据
    function loadBusinessData(businessId) {
        $.get('../businessControl/getBusinessDetail', {bId: businessId}, function(data) {
            if (data) {
                $('#bId').val(data.bId);
                $('#bName').val(data.bName);
                $('#description').val(data.description);

                // 格式化日期
                if (data.startDate) {
                    $('#startDateStr').val(formatDateForInput(data.startDate));
                }
                if (data.endDate) {
                    $('#endDateStr').val(formatDateForInput(data.endDate));
                }

                $('#status').val(data.status);
                $('#money').val(data.money);
            }
        }).fail(function() {
            alert('加载业务数据失败');
        });
    }

    // 日期格式化
    function formatDateForInput(dateString) {
        const date = new Date(dateString);
        return date.toISOString().split('T')[0];
    }

    // 更新业务
    function updateBusiness() {
        const formData = {
            bId: $('#bId').val(),
            bName: $('#bName').val(),
            description: $('#description').val(),
            startDateStr: $('#startDateStr').val(),
            endDateStr: $('#endDateStr').val(),
            status: $('#status').val(),
            money: $('#money').val()
        };

        $.get('../businessControl/update', formData, function(response) {
            if (response) {
                alert('编辑成功');
                window.location.href = 'list.html';
            } else {
                alert('编辑失败');
            }
        }).fail(function() {
            alert('请求失败，请检查网络连接');
        });
    }

    // 表单验证
    function validateForm() {
        let isValid = true;

        // 验证业务名称
        if (!$('#bName').val().trim()) {
            alert('请输入业务名称');
            isValid = false;
        }

        // 验证开始日期
        if (!$('#startDateStr').val()) {
            alert('请选择开始日期');
            isValid = false;
        }

        // 验证结束日期
        if (!$('#endDateStr').val()) {
            alert('请选择结束日期');
            isValid = false;
        }

        // 验证日期顺序
        const startDate = new Date($('#startDateStr').val());
        const endDate = new Date($('#endDateStr').val());
        if (startDate > endDate) {
            alert('开始日期不能晚于结束日期');
            isValid = false;
        }

        // 验证状态
        if (!$('#status').val()) {
            alert('请选择状态');
            isValid = false;
        }

        // 验证预计收入
        const money = parseFloat($('#money').val());
        if (isNaN(money) || money <= 0) {
            alert('请输入有效的预计收入');
            isValid = false;
        }

        return isValid;
    }
</script>
</body>
</html>